<?php

class Theme extends Controller {
	
	function Theme()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->database();
		
		$data['title'] = "GCMS >> Themes";
		$data['heading'] = "Themes";
		$data['style'] = base_url().'system/application/views/content.css';
		
		$query = $this->db->query('SELECT * FROM gcms_options WHERE option_name="theme"');
		$row = $query->row();
		$data['theme'] = $row->option_value;
		
		$data['header'] = $this->load->view('header', $data, true);
		$data['footer'] = $this->load->view('footer', $data, true);
		$this->_login_check($data, 'theme_view');

	}
	
	function edit()
	{
		$this->load->helper('file');
		$this->load->helper('directory');
		
		$data['title'] = "GCMS >> Themes >> Edit";
		$data['heading'] = "Themes >> Edit";
		$data['style'] = base_url().'system/application/views/content.css';
		$data['theme'] = $this->uri->segment(3);
		$data['open_file'] = $this->uri->segment(4);
		
		$data['header'] = $this->load->view('header', $data, true);
		$data['footer'] = $this->load->view('footer', $data, true);
		$this->_login_check($data, 'theme_edit_view');
	}
	
	function edit2()
	{
		$this->load->helper('file');
		
		$content = $_POST['content'];
		$theme = $_POST['theme'];
		$file = $_POST['file'];
		
		write_file('./themes/'.$theme.'/'.$file, $content);
		redirect('/theme/edit/'.$theme.'/'.$file, 'refresh');
	}
	
	function choose()
	{
		$this->load->database();
		
		$theme = $this->uri->segment(3);
		$data = array(
			   'option_value' => $theme
            );
		
		$this->db->where('option_name', 'theme');
		$this->db->update('gcms_options', $data); 
		redirect('/theme', 'refresh');
	}
	
	function _login_check($data, $dest)
	{
		$this->load->library('session');
		$this->load->library('validation');
		$logged_in = $this->session->userdata('logged_in');
		if(!$logged_in){
			$this->load->view('login', $data);
		} else {
			$this->load->view($dest, $data);
		}
	}
	
}
?>